package com.htc.android.footprints.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.utils.StorageUtil;
import com.htc.android.footprints.vo.Footprint;
import java.io.File;
import java.io.IOException;

public class MediaControlPanelViewMode extends LinearLayout
  implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener
{
  public static final int IDLE_STATE = 0;
  public static final int INTERNAL_ERROR = 2;
  private static final int MSG_REFRESH = 1001;
  private static final int MSG_RESET = 1002;
  private static final int MSG_UPDATE_UI = 1000;
  public static final int NO_ERROR = 0;
  public static final int PAUSE_STATE = 2;
  public static final int PLAYING_STATE = 1;
  public static final int SDCARD_ACCESS_ERROR = 1;
  private static final String TAG = "MediaControlPanelViewMode";
  private Context mContext;
  public long mCurrentPosition = 0L;
  TextView mCurrentTime = null;
  private Footprint mFootprint = null;
  private long mLastSeekEventTime;
  OnStateChangedListener mOnStateChangedListener = null;
  ImageView mPlayButton = null;
  View.OnClickListener mPlayButtonClickListener = new View.OnClickListener()
  {
    public void onClick(View paramView)
    {
      if (((MediaControlPanelViewMode.this.mVoiceFile == null) || (MediaControlPanelViewMode.this.mVoiceFile.exists())) || (MediaControlPanelViewMode.this.mState == 0))
        MediaControlPanelViewMode.this.startPlayback();
      while (true)
      {
        MediaControlPanelViewMode.this.queueNextRefresh(MediaControlPanelViewMode.access$100(MediaControlPanelViewMode.this));
        return;
        if (MediaControlPanelViewMode.this.mState == 2)
        {
          MediaControlPanelViewMode.this.togglePlayback();
          continue;
        }
        if (MediaControlPanelViewMode.this.mState != 1)
          continue;
        MediaControlPanelViewMode.this.togglePlayback();
      }
    }
  };
  Handler mPlayHandler = new Handler()
  {
    public void handleMessage(Message paramMessage)
    {
      switch (paramMessage.what)
      {
      default:
        super.handleMessage(paramMessage);
      case 1000:
      case 1001:
      case 1002:
      }
      while (true)
      {
        return;
        MediaControlPanelViewMode.this.mCurrentTime.setText(MediaControlPanelViewMode.this.setupTimer(MediaControlPanelViewMode.this.mCurrentPosition));
        MediaControlPanelViewMode.this.mTotoalTime.setText(MediaControlPanelViewMode.this.setupTimer(MediaControlPanelViewMode.this.mVoiceLength));
        continue;
        MediaControlPanelViewMode.this.queueNextRefresh(MediaControlPanelViewMode.access$100(MediaControlPanelViewMode.this));
        continue;
        MediaControlPanelViewMode.this.resetMediaPanel();
        MediaControlPanelViewMode.this.mCurrentTime.setText(MediaControlPanelViewMode.this.setupTimer(-1L));
        MediaControlPanelViewMode.this.mTotoalTime.setText(MediaControlPanelViewMode.this.setupTimer(-1L));
      }
    }
  };
  private MediaPlayer mPlayer = null;
  private long mPosOverride = -1L;
  SeekBar mProgress = null;
  SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener()
  {
    public void onProgressChanged(SeekBar paramSeekBar, int paramInt, boolean paramBoolean)
    {
      if ((MediaControlPanelViewMode.this.mState == 0) && (StorageUtil.hasExternalStorage()))
        MediaControlPanelViewMode.this.mPlayHandler.post(new Runnable()
        {
          public void run()
          {
            MediaControlPanelViewMode.access$602(MediaControlPanelViewMode.this, MediaControlPanelViewMode.this.mVoiceLength * MediaControlPanelViewMode.this.mProgress.getProgress() / 1000L);
            MediaControlPanelViewMode.this.mCurrentTime.setText(MediaControlPanelViewMode.this.setupTimer(MediaControlPanelViewMode.this.mPosOverride));
            MediaControlPanelViewMode.access$602(MediaControlPanelViewMode.this, -1L);
          }
        });
      if (MediaControlPanelViewMode.this.mPlayer == null);
      while (true)
      {
        return;
        if (paramBoolean)
        {
          long l = SystemClock.elapsedRealtime();
          MediaControlPanelViewMode.access$602(MediaControlPanelViewMode.this, MediaControlPanelViewMode.this.mVoiceLength * paramInt / 1000L);
          if (l - MediaControlPanelViewMode.this.mLastSeekEventTime <= 250L)
            continue;
          MediaControlPanelViewMode.access$802(MediaControlPanelViewMode.this, l);
          try
          {
            MediaControlPanelViewMode.this.mPlayer.seekTo((int)MediaControlPanelViewMode.this.mPosOverride);
          }
          catch (Exception localException)
          {
            localException.printStackTrace();
          }
          continue;
        }
      }
    }

    public void onStartTrackingTouch(SeekBar paramSeekBar)
    {
      if (MediaControlPanelViewMode.this.mPlayer == null);
      while (true)
      {
        return;
        ((Vibrator)MediaControlPanelViewMode.this.mContext.getSystemService("vibrator")).vibrate(30L);
        MediaControlPanelViewMode.access$802(MediaControlPanelViewMode.this, 0L);
      }
    }

    public void onStopTrackingTouch(SeekBar paramSeekBar)
    {
      if ((MediaControlPanelViewMode.this.mState == 0) && (StorageUtil.hasExternalStorage()))
        MediaControlPanelViewMode.this.mPlayHandler.post(new Runnable(paramSeekBar)
        {
          public void run()
          {
            MediaControlPanelViewMode.access$602(MediaControlPanelViewMode.this, MediaControlPanelViewMode.this.mVoiceLength * this.val$seekBar.getProgress() / 1000L);
            MediaControlPanelViewMode.this.mCurrentTime.setText(MediaControlPanelViewMode.this.setupTimer(MediaControlPanelViewMode.this.mPosOverride));
            MediaControlPanelViewMode.access$602(MediaControlPanelViewMode.this, -1L);
          }
        });
      if (MediaControlPanelViewMode.this.mPlayer == null);
      while (true)
      {
        return;
        try
        {
          if (MediaControlPanelViewMode.this.mPlayer.getCurrentPosition() != MediaControlPanelViewMode.this.mPosOverride)
            MediaControlPanelViewMode.this.mPlayer.seekTo((int)MediaControlPanelViewMode.this.mPosOverride);
          MediaControlPanelViewMode.access$602(MediaControlPanelViewMode.this, -1L);
        }
        catch (Exception localException)
        {
          while (true)
          {
            Log.e("MediaControlPanelViewMode", "seekbar tracking error");
            localException.printStackTrace();
          }
        }
      }
    }
  };
  int mState = 0;
  private StorageUtil mStorageUtil;
  TextView mTotoalTime = null;
  File mVoiceFile = null;
  public long mVoiceLength = -1L;

  public MediaControlPanelViewMode(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.mContext = paramContext;
    initView();
  }

  private long calVoiceLength()
  {
    long l;
    if (this.mFootprint == null)
      l = -1L;
    while (true)
    {
      return l;
      try
      {
        MediaPlayer localMediaPlayer = new MediaPlayer();
        localMediaPlayer.setDataSource(this.mVoiceFile.getAbsolutePath());
        localMediaPlayer.prepare();
        this.mVoiceLength = localMediaPlayer.getDuration();
        this.mCurrentPosition = localMediaPlayer.getCurrentPosition();
        localMediaPlayer.release();
        l = this.mVoiceLength;
      }
      catch (IllegalArgumentException localIllegalArgumentException)
      {
        while (true)
          localIllegalArgumentException.printStackTrace();
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
        {
          localIllegalStateException.printStackTrace();
          this.mVoiceLength = -1L;
          this.mCurrentPosition = -1L;
        }
      }
      catch (IOException localIOException)
      {
        while (true)
        {
          localIOException.printStackTrace();
          this.mVoiceLength = -1L;
          this.mCurrentPosition = -1L;
        }
      }
    }
  }

  private void controlScreenKeepOn(int paramInt)
  {
    if (paramInt == 0)
      ((Activity)getContext()).getWindow().clearFlags(128);
    while (true)
    {
      return;
      ((Activity)getContext()).getWindow().addFlags(128);
    }
  }

  private void queueNextRefresh(long paramLong)
  {
    Message localMessage = this.mPlayHandler.obtainMessage(1001);
    this.mPlayHandler.removeMessages(1001);
    this.mPlayHandler.sendMessageDelayed(localMessage, paramLong);
  }

  private long refreshNow()
  {
    long l1;
    if (this.mPlayer == null)
    {
      l1 = 500L;
      return l1;
    }
    while (true)
    {
      long l3;
      try
      {
        if (this.mPosOverride >= 0L)
          continue;
        long l2 = this.mPlayer.getCurrentPosition();
        l3 = 1000L - l2 % 1000L;
        if ((l2 < 0L) || (this.mVoiceLength <= 0L))
          continue;
        this.mCurrentTime.setText(setupTimer(l2));
        if (!this.mPlayer.isPlaying())
          continue;
        this.mCurrentTime.setVisibility(0);
        this.mProgress.setProgress((int)(1000L * l2 / this.mVoiceLength));
        break label202;
        l2 = this.mPosOverride;
        continue;
        int i = this.mCurrentTime.getVisibility();
        TextView localTextView = this.mCurrentTime;
        if (i != 4)
          break label208;
        j = 0;
        localTextView.setVisibility(j);
        l3 = 500L;
        continue;
        this.mCurrentTime.setText("--:--");
        this.mProgress.setProgress(1000);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        l1 = 500L;
      }
      break;
      label202: l1 = l3;
      break;
      label208: int j = 4;
    }
  }

  private void resetMediaPanel()
  {
    stopPlayback();
    this.mProgress.setProgress(0);
    this.mCurrentTime.setText(setupTimer(0L));
    this.mPlayButton.setImageResource(2130837800);
    this.mPlayHandler.removeMessages(1001);
  }

  private void setError(int paramInt)
  {
    if (this.mOnStateChangedListener != null)
      this.mOnStateChangedListener.onError(paramInt);
  }

  private void setIcon(int paramInt)
  {
    switch (paramInt)
    {
    default:
    case 0:
    case 2:
    case 1:
    }
    while (true)
    {
      return;
      this.mPlayButton.setImageResource(2130837800);
      continue;
      this.mPlayButton.setImageResource(2130837800);
      continue;
      this.mPlayButton.setImageResource(2130837801);
    }
  }

  private void setState(int paramInt)
  {
    if (paramInt == this.mState);
    while (true)
    {
      return;
      this.mState = paramInt;
      setIcon(this.mState);
      controlScreenKeepOn(this.mState);
      signalStateChanged(this.mState);
    }
  }

  private String setupTimer(long paramLong)
  {
    if (paramLong < 0L);
    Object[] arrayOfObject;
    for (String str = "--:--"; ; str = String.format("%02d:%02d", arrayOfObject))
    {
      return str;
      arrayOfObject = new Object[2];
      arrayOfObject[0] = Long.valueOf(paramLong / 1000L / 60L);
      arrayOfObject[1] = Long.valueOf(paramLong / 1000L % 60L);
    }
  }

  private void showToast(String paramString)
  {
    if (paramString != null)
      Toast.makeText(this.mContext, paramString, 3000).show();
  }

  private void signalStateChanged(int paramInt)
  {
    if (this.mOnStateChangedListener != null)
      this.mOnStateChangedListener.onStateChanged(paramInt);
  }

  private void startPlayback()
  {
    this.mPlayer = new MediaPlayer();
    if ((this.mVoiceFile == null) || (this.mVoiceFile.length() == 0L))
    {
      if ((this.mFootprint == null) || (!new File(this.mFootprint.mAudioPath).exists()))
        break label183;
      this.mVoiceFile = new File(this.mFootprint.mAudioPath);
    }
    try
    {
      this.mPlayer.setDataSource(this.mVoiceFile.getAbsolutePath());
      this.mPlayer.setOnCompletionListener(this);
      this.mPlayer.setOnErrorListener(this);
      this.mPlayer.prepare();
      if ((this.mState == 0) && (this.mProgress.getProgress() != 0))
      {
        this.mPosOverride = (this.mVoiceLength * this.mProgress.getProgress() / 1000L);
        this.mPlayer.seekTo((int)this.mPosOverride);
        this.mPosOverride = -1L;
      }
      this.mPlayer.start();
      setState(1);
      label183: return;
    }
    catch (IllegalArgumentException localIllegalArgumentException)
    {
      while (true)
      {
        setError(2);
        this.mPlayer = null;
      }
    }
    catch (IOException localIOException)
    {
      while (true)
      {
        setError(1);
        this.mPlayer = null;
      }
    }
  }

  private void togglePlayback()
  {
    if (this.mPlayer == null);
    while (true)
    {
      return;
      if (this.mState != 0)
      {
        try
        {
          if (!this.mPlayer.isPlaying())
            break label48;
          this.mPlayer.pause();
          setState(2);
        }
        catch (IllegalStateException localIllegalStateException)
        {
          localIllegalStateException.printStackTrace();
        }
        continue;
        label48: this.mPlayer.start();
        setState(1);
        continue;
      }
    }
  }

  public void closeMediaPanel()
  {
    if (this.mPlayer != null)
      stopPlayback();
    this.mVoiceFile = null;
    this.mProgress.setProgress(0);
    this.mCurrentTime.setText(setupTimer(0L));
    setIcon(this.mState);
    this.mPlayHandler.removeMessages(1001);
  }

  public void disableMediaControl()
  {
    this.mPlayButton.setEnabled(false);
    this.mProgress.setEnabled(false);
    this.mProgress.setPressed(false);
  }

  public int getCurrentState()
  {
    return this.mState;
  }

  void initView()
  {
    addView((ViewGroup)((LayoutInflater)this.mContext.getSystemService("layout_inflater")).inflate(2130903067, null));
    this.mTotoalTime = ((TextView)findViewById(2131493000));
    this.mTotoalTime.setText(setupTimer(0L));
    this.mCurrentTime = ((TextView)findViewById(2131492999));
    this.mCurrentTime.setText(setupTimer(0L));
    this.mPlayButton = ((ImageView)findViewById(2131492992));
    this.mPlayButton.setOnClickListener(this.mPlayButtonClickListener);
    this.mProgress = ((SeekBar)findViewById(2131493001));
    this.mProgress.setThumb(getResources().getDrawable(2130837692));
    this.mProgress.setOnSeekBarChangeListener(this.mSeekBarChangeListener);
    setIcon(this.mState);
  }

  public void onCompletion(MediaPlayer paramMediaPlayer)
  {
    stopPlayback();
    this.mProgress.setProgress(0);
    this.mCurrentTime.setText(setupTimer(0L));
    this.mPlayButton.setImageResource(2130837800);
    this.mPlayHandler.removeMessages(1001);
    this.mPlayHandler.sendEmptyMessage(1000);
  }

  public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2)
  {
    setError(paramInt1);
    this.mPlayButton.setImageResource(2130837800);
    return false;
  }

  public void setAudioData(Footprint paramFootprint)
  {
    if (paramFootprint == null);
    while (true)
    {
      return;
      this.mFootprint = paramFootprint;
      if (!FootprintUtils.checkAudioData(paramFootprint))
        continue;
      this.mVoiceFile = new File(paramFootprint.mAudioPath);
      MediaPlayer localMediaPlayer = new MediaPlayer();
      try
      {
        localMediaPlayer.setDataSource(this.mVoiceFile.getAbsolutePath());
        localMediaPlayer.prepare();
        this.mVoiceLength = localMediaPlayer.getDuration();
        this.mCurrentPosition = localMediaPlayer.getCurrentPosition();
        localMediaPlayer.release();
        this.mPlayHandler.sendEmptyMessage(1000);
      }
      catch (IllegalArgumentException localIllegalArgumentException)
      {
        while (true)
        {
          localIllegalArgumentException.printStackTrace();
          this.mVoiceLength = -1L;
          this.mCurrentPosition = 0L;
        }
      }
      catch (IllegalStateException localIllegalStateException)
      {
        while (true)
        {
          localIllegalStateException.printStackTrace();
          this.mVoiceLength = -1L;
          this.mCurrentPosition = 0L;
        }
      }
      catch (IOException localIOException)
      {
        while (true)
        {
          localIOException.printStackTrace();
          this.mVoiceLength = -1L;
          this.mCurrentPosition = 0L;
        }
      }
    }
  }

  public void stopPlayback()
  {
    if (this.mPlayer == null);
    while (true)
    {
      return;
      this.mPlayer.stop();
      this.mPlayer.release();
      this.mPlayer = null;
      setState(0);
    }
  }

  public static abstract interface OnStateChangedListener
  {
    public abstract void onError(int paramInt);

    public abstract void onStateChanged(int paramInt);
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.view.MediaControlPanelViewMode
 * JD-Core Version:    0.6.0
 */